package com.jd.mysql.mgr.controller.mgrmachine;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.jd.mysql.mgr.pojo.MgrMachine;
import com.jd.mysql.mgr.service.IMgrDatabaseService;
import com.jd.mysql.mgr.service.IMgrMachineService;
import com.jd.mysql.mgr.util.GlobalStaticVariables;
import com.jd.mysql.mgr.util.PageUtil;
import com.jd.mysql.mgr.util.ParamUtil;
import com.jd.mysql.mgr.util.StringUtil;

public class MgrMachineController extends MultiActionController {
	
	private static Integer start=0;
	private static Integer limit=GlobalStaticVariables.limit;
	private IMgrMachineService mgrMachineService;
	private IMgrDatabaseService mgrDatabaseService;
	

	public void setMgrDatabaseService(IMgrDatabaseService mgrDatabaseService) {
		this.mgrDatabaseService = mgrDatabaseService;
	}

	public void setMgrMachineService(IMgrMachineService mgrMachineService) {
		this.mgrMachineService = mgrMachineService;
	}
	
	public ModelAndView mgrMachineList(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String ip = ParamUtil.getStringParam(request, "ip");
		String index = request.getParameter("index");
		String msg = (String) request.getAttribute("msg");
		Map<String,Object> map = new HashMap<String,Object>();
		Integer start = PageUtil.setPageParam(map,index, limit);
		Integer groupId = (Integer)request.getSession().getAttribute("group");
		Long totalCount = mgrMachineService.getTotalCount(groupId,true,ip);
		List<MgrMachine> dtoList = mgrMachineService.getMgrMachineList(start, limit, true, ip,groupId);
		map.put("totalCount", totalCount);
		map.put("dtoList",dtoList);
		map.put("msg", msg);
		map.put("ip", ip==null?"":ip);
		return new ModelAndView("machine/machineList","map",map);
	}
	
	public ModelAndView deleteMgrMachine(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String ids = request.getParameter("machineId");
		Integer id = 0;
		String msg = null;
		if(!StringUtil.isNull(ids)){
			id = Integer.parseInt(ids);
		}
		MgrMachine mgrMachine  = mgrMachineService.findMgrMachineById(id);
		if(mgrMachine!=null){
			Long totalCount = mgrDatabaseService.getTotalCountByMgrDatabase(mgrMachine);
			if(totalCount==0){
				mgrMachineService.deleteMgrMachine(mgrMachine);	
			}else{
				msg = "机器存在关联数据库，不允许删除";
			}
		}else{
			msg = "机器不存在";
		}
		request.setAttribute("msg", msg);
		return this.mgrMachineList(request, response);
	}
	
	public ModelAndView disableMgrMachine(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String ids = request.getParameter("machineId");
		Integer id = 0;
		if(!StringUtil.isNull(ids)){
			id = Integer.parseInt(ids);
		}
		MgrMachine mgrMachine  = mgrMachineService.findMgrMachineById(id);
		if(mgrMachine!=null){
			mgrMachine.setYn(false);
			mgrMachineService.updateMgrMachine(mgrMachine);
		}
		return this.mgrMachineList(request, response);
	}
	
	public ModelAndView editMachine(HttpServletRequest request,HttpServletResponse response) throws Exception {
		String ids = request.getParameter("machineId");
		Integer id = 0;
		if(!StringUtil.isNull(ids)){
			id = Integer.parseInt(ids);
		}
		MgrMachine mgrMachine  = mgrMachineService.findMgrMachineById(id);
		if(mgrMachine!=null){
			return new ModelAndView("machine/addMgrMachine","mgrMachine",mgrMachine);
		}
		return null;
	}
	
}
